using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Text;

public partial class zmEvoDico : System.Web.UI.MasterPage
{
    private string _M1;

    public string M1
    {
        get { return _M1; }
        set { _M1 = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        TreeView1.Visible=(Membership.GetUser() != null);
        
    }
    protected void SiteMapDataSource1_DataBinding(object sender, EventArgs e)
    {

    }
    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
        
    }

    protected void TreeView1_DataBound(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                MembershipUser currentUser = Membership.GetUser();
                StringBuilder sbModuleName = new StringBuilder();
                if ((currentUser != null) && (!Roles.IsUserInRole("administrator")))
                {
                    Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
                    string sqlCommand = string.Format("SELECT EVOL_Module.ModuleName FROM EVOL_UserPermission INNER JOIN EVOL_Module ON EVOL_UserPermission.ModuleID = EVOL_Module.ID INNER JOIN EVOL_User ON EVOL_UserPermission.UserID = EVOL_User.ID WHERE (EVOL_UserPermission.IsVisiable = 0) and EVOL_User.Login='{0}'", currentUser.UserName);

                    using (IDataReader reader = db.ExecuteReader(CommandType.Text, sqlCommand))
                    {

                        while (reader.Read())
                        {
                            sbModuleName.Append(reader[0].ToString());
                            sbModuleName.Append(";");
                        }
                    }

                }
                TreeNodeCollection tnc = TreeView1.Nodes;
                foreach (string module in sbModuleName.ToString().Split(';'))
                {
                    if (!string.IsNullOrEmpty(module))
                    {
                        FindAndRemoveNode(tnc, module);
                        
                    }
                }

            }
            catch (Exception ex)
            {
                TreeView1.Visible = false;
            }
        }
    }

    private void FindAndRemoveNode(TreeNodeCollection treeNodeCollection, string p)
    {
        TreeNode findNode = null; 
        foreach (TreeNode node in treeNodeCollection)
        {
            if (node.ChildNodes.Count > 0)
                FindAndRemoveNode(node.ChildNodes, p);
            else if (node.Text == p)
            {
                //if (node.Parent != null)
                findNode = node;
                break;
                    
            }
        }
        if(findNode!=null)
            treeNodeCollection.Remove(findNode);
    }
}
